<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Extracted extends Auth_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('extracted_mdl');
    }

	/**
	 * 提现表首页（管理员）
	 */
    public function index(){

    }

	/**
	 * 用户查看提现记录
	 */
	public function show(){

		$url = '';
		$this->db->start_cache();

		if($_SESSION['user']['status'] == 3){
            $this->db->where('user_id',$_SESSION['user']['id']);
        }

		$s = $this->input->get('s');
		if(!empty($s)){
			$this->db->where('status',$s);
			$url .= "s=$s&";
		}
		$this->db->stop_cache();

		if(!empty($url)){
			$url = '?'.substr($url, 0 , -1);
		}

		$this->load->library('pagination');

		$config['base_url'] = site_url('extracted/show').$url;
        $config['total_rows'] = $this->extracted_mdl->get_count();
        $config['per_page'] = 10;
        $config['page_query_string'] = TRUE;
        $config['first_link'] = '首页';
        $config['last_link'] = '尾页';
        $config['next_link'] = '下一页';
        $config['prev_link'] = '上一页';
        $config['anchor_class'] = 'current';

		$this->pagination->initialize($config);
        $data['page'] = $this->pagination->create_links();

		$per_page = $this->input->get('per_page');
        $this->db->limit($config['per_page'],intval($per_page));
        $this->db->order_by('id desc');
        $list = $this->extracted_mdl->select();
		$this->db->flush_cache();
		$this->load->model('bank_mdl');
		foreach($list as $k => $v){
			$this->db->where('id',$v['bank_id']);
			$list[$k]['bank'] = $this->bank_mdl->get_one();
			switch ($v['status']) {
				case '1':
					$list[$k]['status_name'] = '正在申请提现';
					break;
				case '2':
					$list[$k]['status_name'] = '提现受理，正在转账中..';
					break;
				case '3':
					$list[$k]['status_name'] = '提现失败！原因是：“'.$v['error_log'].'”';
					break;
				case '4':
					$list[$k]['status_name'] = '提现成功！';
					break;
				default:
					$list[$k]['status_name'] = '未知状态！';
					break;
			}
		}
		$data['list'] = $list;
		$this->_view('show',$data);
	}

	/**
	 * 用户确认收款
	 */
	public function confirm(){
		if($_SESSION['group'] != 3){
            show_error('没有权限！');
        }

        if(!$this->input->is_ajax_request()){
        	die('参数错误！');
        }

		$eid = $this->input->post('eid');

		$this->db->where('id',$eid);
		$this->db->where('status',2);
		$res = $this->extracted_mdl->get_one();

		if(empty($res)){
			die('该数据不存在');
		}
		$this->db->set('status',4);
		$this->db->where('id',$eid);
		$r = $this->extracted_mdl->update();

		//加入信息表
		$mess = '确认收款成功！感谢您的支持！';
		$this->load->model('mess_mdl');
		$this->mess_mdl->send_message($mess);
		echo $r === TRUE ? '1' : '系统错误！';
	}

	/**
	 * 确认用户提现
	 */
	public function handle(){
		if($_SESSION['group'] == 3){
            show_error('没有权限！');
        }
		if(!$this->input->is_ajax_request()){
        	die('参数错误！');
        }
		$eid = $this->input->post('eid');
		$this->db->where('id',$eid);
		$this->db->where('status',1);
		$res = $this->extracted_mdl->get_one();

		if(empty($res)){
			die('数据不存在');
		}

		//扣除用户余额并写入账单
		$this->load->model('uinfo_mdl');
		$this->load->model('bill_mdl');
		//判断用户余额是否支持提现
		$this->db->where('id',$res['user_id']);
		$this->db->select('money');
		$res_uinfo = $this->uinfo_mdl->get_one();
		if($res_uinfo['money'] - $res['money'] < 0){
			$this->db->where('id',$eid);
			$this->db->set('status',3);
			$this->db->set('error_log','余额不足，不能提现！');
			$r = $this->extracted_mdl->update();
			die('该用户余额不足，不能提现！');
		}

		$this->db->trans_start();
		$this->db->where('id',$res['user_id']);
		$this->db->set('money','money-'.$res['money'],FALSE);
		$this->uinfo_mdl->update();

		$this->bill_mdl->add_bill($res['user_id'],$res['money']*-1,'用户提现');

		$this->db->where('id',$eid);
		$this->db->set('status',2);
		$r = $this->extracted_mdl->update();
		$this->db->trans_complete();
		echo $r == TRUE?'1':'数据处理失败，1请稍后再试！';
	}

	/**
	 * 添加提现表
	 */
	public function create(){
		if($_SESSION['group'] != 3){
            show_error('没有权限！');
        }

        if(!$this->input->is_ajax_request()){
        	die('参数错误！');
        }

		$post = $this->input->post();

		if($post['money'] < 1000 || !is_numeric($post['money'])){
			die('请输入大于1000的提现金额');
		}

		//Model
		$this->load->model('bank_mdl');
		$this->db->where('id',intval($post['bid']));
		$res_bank = $this->bank_mdl->get_one();

		if(empty($res_bank)){
			die('该银行卡不存在！');
		}

		$data = array(
			'user_id' => $_SESSION['user']['id'],
			'money' => $post['money'],
			'status' => 1,
			'create_time' => $_SERVER['REQUEST_TIME'],
			'bank_id' => $post['bid']
		);
		$res = $this->extracted_mdl->create($data);

		//管理员消息提醒
		$this->load->model('mess_mdl');
		$this->mess_mdl->send_admin('新增一个用户提款信息！&nbsp;&nbsp;&nbsp;<a href="'.site_url('extracted/show?s=1').'">查看</a>');

		echo $res == true ? '1' : '服务器错误，请稍后再试';
	}


}

/* End of file Config.php */
/* Location: ./application/controllers/Config.php */